package com.hanhai.zrb.mobile.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.zrb.api.client.facade.MessageFacade;
import com.hanhai.zrb.api.client.result.MessageResult;
import com.hanhai.zrb.model.message.MessageReceive;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

//应该没有使用
@Deprecated
@LoginNeedless
@RequestMapping("/sysMessage")
@Controller
public class SysMessageApiController {

	@Resource
	private MessageFacade messageFacade;
	
	@RequestMapping("/readMessage")
	public void readMessage(HttpServletRequest request,HttpServletResponse response,Long id){
		String receiverUid = request.getParameter("receiverUid");
		long uid = Long.parseLong(receiverUid);
		MessageResult messageResult = this.messageFacade.readMessage(uid, id);		
	}
	
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "消息列表",  response = List.class, notes = "消息列表")
	@RequestMapping(value="list")
	public String getProByAutoId(HttpServletRequest request,HttpServletResponse response,Model model,
			Long maxId,Integer limit) throws Exception{
		String receiverUid = request.getParameter("receiverUid");
		long uid = 0;
		if( StringUtils.isNotBlank(receiverUid)){
			uid = Long.parseLong(receiverUid);
		}
		if(maxId == null){
			maxId = Long.MAX_VALUE;
		}
		if(limit == null||limit<=0){
			limit = 8;
		}
		MessageResult messageResult = this.messageFacade.queryReceiveMessageByMaxId(uid, null, maxId, limit);//(loginUser.getUid(), id);		
		List<MessageReceive> receiveList = messageResult.getReceiveList();
		model.addAttribute("receiveList", receiveList);
		model.addAttribute("reuid", uid);
		return "sysmessage/sysMessage";
	}
	
	@RequestMapping(value = "/next")
	@ResponseBody
	public Object bbsReplaycheck(HttpServletRequest request, Model model) {
		String maxIds = request.getParameter("maxIds");
		String receiverUid = request.getParameter("receiverUid");
		long uid = Long.parseLong(receiverUid);
		long maxId = Long.parseLong(maxIds);
		MessageResult messageResult = this.messageFacade.queryReceiveMessageByMaxId(uid, null, maxId, 8);//sql无数据会报错，但不影响程序
		List<MessageReceive> receiveList = messageResult.getReceiveList();
		Object data = JSON.toJSON(receiveList);
		return data;
	}
	
}
